package cn.fresh.xuanyuan.module.member.controller.app.user;

import cn.fresh.xuanyuan.framework.common.exception.ErrorCode;
import cn.fresh.xuanyuan.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.framework.security.core.annotations.PreAuthenticated;
import cn.fresh.xuanyuan.module.member.service.user.MemberCollectService;
import cn.fresh.xuanyuan.module.member.vo.MemberCollectVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Map;

import static cn.fresh.xuanyuan.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户收藏管理")
@RestController
@RequestMapping("/member/like")
@Validated
@Slf4j
public class MemberCollectController {

    @Resource
    private MemberCollectService memberCollectService;

    @PostMapping(value = "/addLike")
    @Operation(summary = "喜欢")
    @PreAuthenticated
    public CommonResult<Integer> addLike(@RequestBody Map<String, Long> map) {
        if(map == null || map.get("spuId") == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        Long userId = getLoginUserId();
        int n = memberCollectService.addLike(map.get("spuId"), userId);
        return CommonResult.success(n);
    }

    @PostMapping(value = "/addCollect")
    @Operation(summary = "收藏")
    @PreAuthenticated
    public CommonResult<Integer> addCollect(@RequestBody Map<String, Long> map) {
        if(map == null || map.get("spuId") == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        Long userId = getLoginUserId();
        int n = memberCollectService.addCollect(map.get("spuId"), userId);
        return CommonResult.success(n);
    }

    @PostMapping(value = "/removeCollect")
    @Operation(summary = "取消收藏")
    @PreAuthenticated
    public CommonResult<Integer> removeCollect(@RequestBody Map<String, Long> map) {
        if(map == null || map.get("id") == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        Long userId = getLoginUserId();
        memberCollectService.removeCollect(map.get("id"), userId);
        return CommonResult.success(1);
    }

    @GetMapping(value = "/myCollectList")
    @Operation(summary = "收藏列表")
    @PreAuthenticated
    public CommonResult<PageResult<MemberCollectVO>> myCollectList(@ModelAttribute PageParam pageParam) {
        Long userId = getLoginUserId();
        PageResult<MemberCollectVO> page = memberCollectService.myCollectList(userId, pageParam);
        return CommonResult.success(page);
    }
}
